package me.dengfengdecao.demo.component;

import me.dengfengdecao.demo.utils.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Reader;
import java.io.Writer;
import java.util.Locale;

/**
 * Created by dengfengdecao on 16/9/28.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    public static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler
    public String  defaultErrorHandler(HttpServletRequest req, HttpSession session, NativeWebRequest nativeWebRequest,
                                    Locale locale, Reader reader, Writer writer, Model model, Exception e)  {

        log.error("defaultErrorHandler: Message : " + e.getMessage());

        model.addAttribute(Const.FAIL_RESPONSE, e.fillInStackTrace().getCause());

        return "error/error";
    }
}
